package com.ssy.lingxi.message.controller;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.message.model.dto.AddSessionDTO;
import com.ssy.lingxi.message.model.dto.ImGetChatRecordDTO;
import com.ssy.lingxi.message.model.dto.ImNotifyDTO;
import com.ssy.lingxi.message.model.vo.HistorySessionVO;
import com.ssy.lingxi.message.model.vo.ImChatRecordVO;
import com.ssy.lingxi.message.model.vo.UserSigVO;
import com.ssy.lingxi.message.service.ImService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @menu im
 * @author fhj
 * @version 2.0.0
 * @date 20201/03/22
 */
@Api(tags = "im")
@RestController
@RequestMapping("/message/im")
public class ImController extends BaseController {

    @Resource
    private ImService imService;
    @Resource
    private HttpServletRequest request;

    @ApiOperation(value = "历史会话")
    @GetMapping(value = "/historySession")
    public Wrapper<List<HistorySessionVO>> historySession(@Valid AddSessionDTO dto) {
        if(dto.getMemberId() != null) {
            Wrapper<Void> wrapper = imService.add(dto.getMemberId(), 1, getSysUser());
            if(wrapper.getCode() == ResponseCode.SUCCESS.getCode()) {
                return imService.all(1, getSysUser());
            }
        }

        return imService.all(1, getSysUser());
    }

    @ApiOperation(value = "获取UserSig")
    @GetMapping(value = "/getUserSig")
    public Wrapper<UserSigVO> getUserSig(String userId) {
        return imService.getUserSig(userId, isLogin() ? getSysUser() : null);
    }

    @ApiOperation(value = "IM回调")
    @PostMapping(value = "/notify")
    public String notify(@RequestBody ImNotifyDTO dto) {
        return imService.notify(dto, request);
    }

    @ApiOperation(value = "获取聊天记录")
    @GetMapping(value = "/getChatRecord")
    public Wrapper<PageData<ImChatRecordVO>> getChatRecord(@Valid ImGetChatRecordDTO dto) {
        return imService.getChatRecord(dto);
    }
}